package com.taoyibao.hongcanting.ui.home.views;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.BaseRelativeLayout;
import com.taoyibao.hongcanting.model.FoodTypeModel;
import com.taoyibao.hongcanting.model.ShoppingCartModel;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;

/**
 * @author TangAnna
 * @description: 购物车列表模块
 * @date :${DATA} 16:24
 */
public class CartListArea extends BaseRelativeLayout<List<ShoppingCartModel>> {
    private RecyclerView mRecyclerView;
    /**
     * 是否显示数量控制器 默认显示
     */
    private boolean isShow = true;
    /**
     * 总数量
     */
    private TextView mTvTotlaCount;

    /**
     * 总价格（优惠后的价格）
     */
    private TextView mTvDiscountPrice;


    public CartListArea(Context context) {
        super(context);
    }

    public CartListArea(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CartListArea(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        mTvTotlaCount = findViewById(R.id.tv_shoppingCartList_total_count);
        mTvDiscountPrice = findViewById(R.id.tv_shoppingCartList_discount_price);
        mRecyclerView = findViewById(R.id.rv_shoppingCart_body);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
    }

    @Override
    public void setData(List<ShoppingCartModel> data) {
        if (data == null || data.size() < 1) {
            return;
        }
        ShoppingCartModel dataModel = data.get(0);//头部数据
        setTotalData(dataModel);
        List<FoodTypeModel> info = dataModel.info;//列表数据
        mRecyclerView.setAdapter(new CommonAdapter<FoodTypeModel>(getContext(), R.layout.item_shopping_cart_shell, info) {
            @Override
            protected void convert(ViewHolder holder, FoodTypeModel foodTypeModel, int position) {
                ItemCartArea itemCartArea = (ItemCartArea) holder.getConvertView();
                itemCartArea.isShowCountController(isShow);
                itemCartArea.setData(foodTypeModel);
            }
        });
    }

    /**
     * 设置上半部分和下半部分的数据
     */
    private void setTotalData(ShoppingCartModel data) {
        if (data == null) {
            return;
        }
        mTvTotlaCount.setText("共" + data.total_num + "份");
        mTvDiscountPrice.setText(CodeUtils.formatAmountUnit(data.discount_price));
    }

    /**
     * 设置是否显示数量控制器 在setData方法之前调用
     *
     * @param show true 显示（默认显示）  false 不显示
     */
    public void isShowCountController(boolean show) {
        isShow = show;
    }

    @Override
    protected int bindLayout() {
        return R.layout.area_cart_list;
    }
}
